/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package atrevaluator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/* Generates for example a following file:
 * 
 * 
set   autoscale                        # scale axes automatically
unset log                              # remove any log-scaling
unset label                            # remove any previous labels
set xtic auto                          # set xtics automatically
set ytic auto                          # set ytics automatically

set term png
set title ""
set xlabel "k"
set ylabel "Precision"
set output '~/kiwi/atr/results/IntroductionToInformationRetrieval/precision.png'
plot  "~/kiwi/atr/results/IntroductionToInformationRetrieval/IBM_GlossEx_ATR_ALGORITHM.dat" using 1 title '' with lines, \
"~/kiwi/atr/results/IntroductionToInformationRetrieval/CValue_ALGORITHM.dat"
 * 
 * /

/**
 *
 * @author iknoth
 */
public class GnuplotExporter {

    private String directory;

    public GnuplotExporter(String directory) {
        this.directory = directory;
    }

    public void export(String filename, String yaxis, String extension) {
        ArrayList<String> dataFiles;

        ReadDirectory rd = new ReadDirectory(directory);
        rd.addExtension(extension);
        dataFiles = rd.getRecursiveList();

        String text = "set autoscale                    # scale axes automatically\n" +
                "unset log                              # remove any log-scaling\n" +
                "unset label                            # remove any previous labels\n" +
                "set xtic auto                          # set xtics automatically\n" +
                "set ytic auto                          # set ytics automatically\n" +
                "set term png\nset title \"\"\n" +
                "set xlabel \"k\"\n" +
                "set ylabel \"" + yaxis + "\"\n";

        text += "set output \"" + directory + "/" + filename + "\"\n";
        text += "plot ";

        for (String file : dataFiles) {

            File f = new File(file);
            text += "\"" + file + "\" using 1 title '" + f.getName() + "' with lines, \\\n";
        }

        text = text.substring(0, text.length() - 4);

        InputOutput io = new InputOutput();
        int lastDot = filename.lastIndexOf('.');
        filename = filename.substring(0, lastDot) + ".p";        
        io.writeStringToFile(directory + "/" + filename, text);
    }

    public void generateGraph() {

        try {

            ReadDirectory rd = new ReadDirectory(this.directory);
            rd.addExtension(".p");
            ArrayList<String> files = rd.getRecursiveList();

            for (String file : files) {

                String[] cmd = new String[2];
                cmd[0] = "gnuplot";
                cmd[1] = file;

                // create and start the process - redirect stderr to stdout
                ProcessBuilder processBuilder = new ProcessBuilder(cmd);
                processBuilder.redirectErrorStream(true);

                // this call does not block
                Process process = processBuilder.start();

                // create the gobbler to read the stdout/stderr from the process
                StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());

                // kick off the separate stream gobbler thread   
                outputGobbler.start();

                // wait for the process to terminate - blocks
                int exitCode = process.waitFor();

                // wait for the gobbler to terminate - blocks
                outputGobbler.join();

                // close all the process IO streams

                process.getInputStream().close();
                process.getOutputStream().close();
                process.getErrorStream().close();
            }

        } catch (IOException e) {
            // ignore error
        } catch (InterruptedException ie) {
            // ignore error
        }

    }
}
